package org.akomantoso.core.data;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.akomantoso.core.data.attribute.Source;
import org.akomantoso.core.io.Externalizable;
import org.akomantoso.core.io.Input;
import org.akomantoso.core.io.Output;
import org.akomantoso.core.io.OutputUtil;
import org.akomantoso.core.io.util.AnalysisUtil;

/**
 * <pre>
 * 	<xsd:element name="analysis">
 * 		<xsd:complexType>
 * 			<xsd:sequence>
 * 				<xsd:element ref="activeModifications" minOccurs="0" maxOccurs="1" />
 * 				<xsd:element ref="passiveModifications" minOccurs="0" maxOccurs="1" />
 * 				<xsd:element ref="judicial" minOccurs="0" maxOccurs="1" />
 * 				<xsd:element ref="parliamentary" minOccurs="0" maxOccurs="1" />
 * 				<xsd:element ref="otherAnalysis" minOccurs="0" maxOccurs="unbounded" />
 * 			</xsd:sequence>
 * 			<xsd:attributeGroup ref="source" />
 * 		</xsd:complexType>
 * 	</xsd:element>
 * </pre>
 * 
 * @author <a href="mailto:jacques.militello@gmail.com">Jacques Militello</a>
 */
public final class Analysis implements Externalizable, Source {

	public static final String ELEMENT = "analysis";
	
	private String _source;
	
	private List<AnalysisElement> _elements;
	
	/**
	 * @see org.akomantoso.core.data.attribute.Source#getSource()
	 */
	public String getSource() {
		return _source;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Source#setSource(java.lang.String)
	 */
	public void setSource(String source) {
		_source = source;
	}
	
	public List<AnalysisElement> getElements() {
		return _elements;
	}

	public void setElements(List<AnalysisElement> elements) {
		_elements = elements;
	}
	
	/**
	 * @see org.akomantoso.core.io.Externalizable#writeExternal(org.akomantoso.core.io.Output)
	 */
	public <T extends Output> void writeExternal(T out) throws IOException {
		out.writeStart(ELEMENT);
		OutputUtil.writeSource(out, this);
		if (_elements != null) {
			for (int i = 0, n = _elements.size(); i < n; i++) {
				_elements.get(i).writeExternal(out);
			}
		}
		out.writeEnd();
	}
 	

	/**
	 * @see org.akomantoso.core.io.Externalizable#readExternal(org.akomantoso.core.io.Input)
	 */
	public <T extends Input> void readExternal(T in) throws IOException {
		_source = in.readAttribute(Source.ATTRIBUTE);
		AnalysisUtil.read(in, _elements = new ArrayList<AnalysisElement>());
	}

}